﻿using System;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.Web;
using System.Xml;
using ScottLane.MediaFeeder.ObjectModel;

namespace ScottLane.MediaFeeder.WebService
{
    /// <summary>
    /// A SyndicationItem representing an Album.
    /// </summary>
    public class AlbumSyndicationItem : SyndicationItem
    {
        private Album album;

        /// <summary>
        /// Initializes a new instance of the AlbumSyndicationItem class with the specified Album.
        /// </summary>
        /// <param name="album">The album.</param>
        public AlbumSyndicationItem(Album album)
        {
            this.album = album;
            this.Title = new TextSyndicationContent(album.Title);
			string albumUrl = Utility.BaseUrl + "/music/albums/" + HttpUtility.UrlEncode(album.AlbumID);
			this.Id = albumUrl;
			this.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(albumUrl)));

            foreach (string genre in album.Genres)
            {
                this.Categories.Add(new SyndicationCategory(genre));
            }
        }

        protected override void WriteElementExtensions(XmlWriter writer, string version)
        {
			if (album.Artist != null)
			{
				writer.WriteElementString("mf:artist", album.Artist);
			}
			if (album.Title != null)
			{
				writer.WriteElementString("mf:title", album.Title);
			}

            writer.WriteElementString("mf:songCount", album.Songs.Count.ToString());

            if (album.ImageFileName != null)
            {
                writer.WriteElementString("mf:imageFileName", album.ImageFileName);
            }

			writer.WriteElementString("mf:duration", album.Duration.ToString());
			writer.WriteElementString("itunes:duration", album.Duration.ToString());
        }
    }
}
